import {
    SUBJECT_TYPE
} from "@/config/enum";
export default {
    namespaced: true,
    state: {
        subjectList: [],
        subjectCartList: []
    },
    getters: {
        cat_danxuanlist({
            subjectCartList
        }) {
            return subjectCartList.filter(r => r.type === SUBJECT_TYPE.DANXUAN)
        },
        cat_duoxuanlist({
            subjectCartList
        }) {
            return subjectCartList.filter(r => r.type === SUBJECT_TYPE.DUOXUAN)
        },
        cat_panduanlist({
            subjectCartList
        }) {
            return subjectCartList.filter(r => r.type === SUBJECT_TYPE.PANDUAN)
        },
        cat_jiandalist({
            subjectCartList
        }) {
            return subjectCartList.filter(r => r.type === SUBJECT_TYPE.JIANDA)
        },
        cartCount(state) {
            return state.subjectCartList.length
        }
    },
    mutations: {
        setSubjectList(state, payload) {
            state.subjectList = payload
        },
        add2Cart(state, payload) {
            if (state.subjectCartList.some(r => r.id === payload.id)) {
                payload.isSuccess = false
            } else {
                state.subjectCartList.push(payload)
                payload.isSuccess = true
            }

        },
        removeFromCart(state,payload){
            state.subjectCartList = state.subjectCartList.filter(r=>r.id !==payload)
        }
    }
}